package cn.ecnu.controller;

import cn.ecnu.common.utils.R;
import cn.ecnu.mapper.ConsultRecordMapper;
import cn.ecnu.mapper.ConsultantMapper;
import cn.ecnu.mapper.SupervisorMapper;
import cn.ecnu.mapper.UserMapper;
import cn.ecnu.pojo.ConsultRecord;
import cn.ecnu.pojo.Consultant;
import cn.ecnu.pojo.Supervisor;
import cn.ecnu.pojo.User;
import cn.ecnu.vo.toFront.ConsultRecordVo;
import cn.ecnu.vo.toFront.RankVo;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import static cn.ecnu.common.constant.BusinessConstant.CONSULTANT_ONGOING_LIST_KEY;
import static cn.ecnu.common.constant.BusinessConstant.SUPERVISOR_ONGOING_COUNT_KEY;

@RestController
@RequestMapping("/admin")
public class AdminController {

    @Resource
    private ConsultantMapper consultantMapper;

    @Resource
    private SupervisorMapper supervisorMapper;

    @Resource
    private ConsultRecordMapper consultRecordMapper;

    @Resource
    private UserMapper userMapper;

    @Resource
    private RedisTemplate<Object, Object> redisTemplate;

    @GetMapping("/getInfo")
    public R getInfo() {

        try {

            HashMap<String, Object> map = new HashMap<>();

            List<Consultant> consultants = consultantMapper.getAllConsultants();
            List<Supervisor> supervisors = supervisorMapper.getAllSupervisor();

            //今日咨询数
            Integer countToday = consultRecordMapper.getAllCountToday();
            if (countToday == null) {
                countToday = 0;
            }
            map.put("countToday", countToday);

            //今日咨询时长
            Integer timeToday = consultRecordMapper.getAllTimeToday();
            if (timeToday == null) {
                timeToday = 0;
            }
            map.put("timeToday", timeToday);

            //一天里每小时的咨询数量
            ArrayList<Integer> list = new ArrayList<>();
            for (int hour = 0; hour <= 23; hour++) {
                Integer hourCount = consultRecordMapper.getTodayHourCount(hour);
                list.add(hourCount);
            }
            map.put("todayCountFlow", list);

            //近七天每天的咨询数量
            list = new ArrayList<>();
            for (int day = 6; day >= 0; day--) {
                Integer count = consultRecordMapper.getDayCountByGap(day);
                list.add(count);
            }
            map.put("weekCountFlow", list);

            //当前正在进行的咨询数量
            int count = 0;
            for (Consultant consultant : consultants) {
                Integer consultantId = consultant.getConsultantId();
                Long size = redisTemplate.opsForList().size(String.format(CONSULTANT_ONGOING_LIST_KEY, consultantId));
                count += size;
            }
            map.put("consultSession", count);

            //当前正在进行的求助数量
            count = 0;
            for (Supervisor supervisor : supervisors) {
                Integer supervisorId = supervisor.getSupervisorId();
                Integer currentSession = (Integer) redisTemplate.opsForValue().get(String.format(SUPERVISOR_ONGOING_COUNT_KEY, supervisorId));
                if (currentSession != null) {
                    count += currentSession;
                }
            }
            map.put("helpSession", count);

            return R.ok().put("data", map);

        } catch (Exception e) {

            e.printStackTrace();

        }

        return R.error();

    }

    @GetMapping("/getRanking")
    public R getRanking() {

        try {

            HashMap<String, Object> map = new HashMap<>();

            List<RankVo> countRank = consultantMapper.getCountRank();
            // userName 和 userAvatar 需要额外填入
            for (RankVo rankVo : countRank) {
                User user = userMapper.getUserById(rankVo.getUserId());
                rankVo.setUserAvatar(user.getUserAvatar());
                rankVo.setUserName(user.getUserName());
            }
            map.put("countRanking", countRank);

            List<RankVo> gradeRank = consultantMapper.getGradeRank();
            // userName 和 userAvatar 需要额外填入
            for (RankVo rankVo : gradeRank) {
                User user = userMapper.getUserById(rankVo.getUserId());
                rankVo.setUserAvatar(user.getUserAvatar());
                rankVo.setUserName(user.getUserName());
            }
            map.put("gradeRanking", gradeRank);

            return R.ok().put("data", map);

        } catch (Exception e) {

            e.printStackTrace();

        }

        return R.error();

    }

}
